//
//  FunctionDefine.m
//  DemoProject
//
//  Created by hao on 2018/7/13.
//  Copyright © 2018年 hao. All rights reserved.
//

#import "CommonFunction.h"

#pragma mark - 屏幕
static CGFloat _kScreenWidth = 0;
static CGFloat _kScreenHeight = 0;
CGFloat MainScreenWidth()
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _kScreenWidth = [UIScreen mainScreen].bounds.size.width;
    });
    return _kScreenWidth;
}

CGFloat MainScreenHeight()
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _kScreenHeight = [UIScreen mainScreen].bounds.size.height;
    });
    return _kScreenHeight;
}

BOOL    isiPhone4Screen(void)
{
    return MainScreenWidth() == 320.0 && MainScreenHeight() == 480.0;
}

BOOL    isiPhone5Screen(void)
{
    return MainScreenWidth() == 320.0 && MainScreenHeight() == 568.0;
}

BOOL    isiPhone8Screen(void)
{
    return MainScreenWidth() == 375.0 && MainScreenHeight() == 667.0;
}

BOOL    isiPhone8plusScreen(void)
{
    return MainScreenWidth() == 414.0 && MainScreenHeight() == 736.0;
}

BOOL    isiPhoneXScreen(void)
{
    //    return kScreenWidth() == 375.0 && kScreenHeight() == 812.0;
    return ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO);
}

BOOL    isiPhoneXRScreen(void)
{
    return ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(828, 1792), [[UIScreen mainScreen] currentMode].size) : NO);
}

BOOL    isiPhoneXSMaxcreen(void)
{
    return ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2688), [[UIScreen mainScreen] currentMode].size) : NO);
}

BOOL    isFullScreen(void)
{
    return isiPhoneXScreen() || isiPhoneXRScreen() || isiPhoneXSMaxcreen();
}

CGFloat kTabBarHeight(void)
{
    return isFullScreen() ? (34.0+49.0) : 49.0;
}

CGFloat kNaviBarHeight(void)
{
    return 44.0;
}

CGFloat kStatusBarHeight(void)
{
    return isFullScreen() ? 44.f : 20.f;
}

CGFloat kStatusBarAndNaviHeight(void)
{
    return kNaviBarHeight() + kStatusBarHeight();
}

CGFloat kX_BottomExtraHeight(void)
{
    return 44.0;
}

#pragma mark - 适配
CGFloat kScreenWidthRatio(void)
{
    // 375(6、6s、7、8、X的屏幕宽度)可更改为其它屏幕的宽度
    return MainScreenWidth()/375.0;
}

CGFloat kScreenHeightRatio(void)
{
    return isFullScreen() ? 1 : MainScreenHeight()/667.0;
}

CGFloat kAdaptedWidth(CGFloat ratio)
{
    // ceilf() 函数为向上取整
    return ceilf(ratio * kScreenWidthRatio());
}

CGFloat kAdaptedHeight(CGFloat ratio)
{
    return ceilf(ratio * kScreenHeightRatio());
}

UIFont  *kAdaptedSystemFont(CGFloat size)
{
    return [UIFont systemFontOfSize:kAdaptedWidth(size)];
}

#pragma mark - 颜色
static const CGFloat  kFBRGBMax         = 255.0f;
static const uint32_t kColorMaskRed     = 0xFF000000;
static const uint32_t kColorMaskGreen   = 0xFF0000;
static const uint32_t kColorMaskBlue    = 0xFF00;
static const uint32_t kColorMaskAlpha   = 0xFF;

UIColor *kColorWithHexRGB(uint32_t rgbValue)
{
    uint32_t newValue = (rgbValue << 8) | kColorMaskAlpha;
    return kColorWithHexRGBA(newValue);
}

UIColor *kColorWithHexRGBA(uint32_t rgbaValue)
{
    //  R   G  B  A
    //0xFF FF FF FF
    uint8_t r = (rgbaValue & kColorMaskRed) >> 24;
    uint8_t g = (rgbaValue & kColorMaskGreen) >> 16;
    uint8_t b = (rgbaValue & kColorMaskBlue) >> 8;
    uint8_t a = (rgbaValue & kColorMaskAlpha) / kFBRGBMax;
    return kColorWithRGBA(r,g,b,a);
}

UIColor *kColorWithRGBA(uint8_t r, uint8_t g, uint8_t b, CGFloat a)
{
    return [UIColor colorWithRed:(r / kFBRGBMax)
                           green:(g / kFBRGBMax)
                            blue:(b / kFBRGBMax)
                           alpha:a];
}

UIColor *kColorWithRGB(uint8_t r, uint8_t g, uint8_t b)
{
    return kColorWithRGBA(r, g, b, 1.0f);
}

UIColor *kRandomColorAlpha(CGFloat alpha)
{
    if (alpha < 0) {
        alpha = 0;
    } else if (alpha > 1) {
        alpha = 1;
    }
    return [UIColor colorWithRed:arc4random()%256/256.0 green:arc4random()%256/256.0 blue:arc4random()%256/256.0 alpha:alpha];
}

UIColor *kRandomColor(void)
{
    return kRandomColorAlpha(1);
}


#pragma mark - 角度弧度转换
/** 由角度转换弧度 */
CGFloat kDegreesToRadian(CGFloat x)
{
    return (M_PI * (x) / 180.0);
}
/** 由弧度转换角度 */
CGFloat kRadianToDegrees(CGFloat radian)
{
    return radian * 180.0 / M_PI;
}


